#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>

#include<iostream>
#include <gst/gst.h>
#include <stdio.h>

#include <sstream>
#include <string>

#define AUDIO_CAPS "application/x-rtp,media=audio, clock-rate=89000, width=16, height=16, encoding-name=L16, payload=96"

using namespace std;

string audioPath;

GstElement *pipeline;

void receiveMp3Audio ()
{

    /*Main object*/
       GMainLoop *loop;

       /*Elements objects*/
       GstElement
                  *source,
                  *decoder,
                  *convert,
                  *sink;

       /*Cap object*/
       GstCaps *caps;

       loop = g_main_loop_new (NULL, FALSE);

       /* Pipeline description*/
       /* gst-launch-0.10 -v udpsrc port=5000 caps="application/x-rtp,media=audio, clock-rate=89000, width=16, height=16, encoding-name=L16,
          payload=96" ! rtpL16depay ! audioconvert ! alsasink sync=false */

       /* Create gstreamer elements */
       pipeline = gst_pipeline_new ("audio-player");
       source   = gst_element_factory_make ("udpsrc","udpsrc");
       decoder  = gst_element_factory_make ("rtpL16depay","rtpL16depay");
       convert  = gst_element_factory_make ("audioconvert","audioconvert");
       sink     = gst_element_factory_make ("alsasink","alsasink");

       if (!pipeline || !source || !decoder || !convert || !sink) {
           g_printerr ("One element could not be created. Exiting.\n");
       }

       caps = gst_caps_from_string (AUDIO_CAPS);

       g_object_set (G_OBJECT (source), "port", 5000, NULL);
       g_object_set (G_OBJECT (source), "caps", caps , NULL);


       /* we add all elements into the pipeline */
       gst_bin_add_many (GST_BIN (pipeline),source, decoder, convert, sink, NULL);

       /* we link the elements together */
       gst_element_link_many (source, decoder, convert, sink, NULL);

       /* Set the pipeline to "playing" state*/
       g_print ("Now playing...\n");
       gst_element_set_state (pipeline, GST_STATE_PLAYING);

       /* Iterate*/
       g_print ("Listening...\n");
       g_main_loop_run (loop);

       /* Stop playback*/
       g_print ("Stopping playback\n");
       gst_element_set_state (pipeline, GST_STATE_NULL);

       /* Delete the pipeline*/
       g_print ("Deleting pipeline\n");
       gst_object_unref (GST_OBJECT (pipeline));
       g_main_loop_unref (loop);


}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QObject::connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->dial,SLOT(setValue(int)));
    QObject::connect(ui->dial,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
}

MainWindow::~MainWindow()
{
    delete ui;

}






/* This method executes when the Stop button is pressed*/
void MainWindow::on_buttonStop_clicked()
{
 gst_element_set_state (pipeline, GST_STATE_PAUSED);

}


/* This method executes when the Play button is pressed*/
void MainWindow::on_buttonPlay_clicked()
{
    receiveMp3Audio();

}
